package com.yuhanginfo.modules.web.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.modules.web.entity.WebFreeWaybill;
import com.yuhanginfo.modules.web.entity.WebStore;
import com.yuhanginfo.modules.web.service.WebFreeWaybillService;
import com.yuhanginfo.modules.web.service.WebStoreService;

/**
 * 包邮设置Controller
 * @author yangs
 * @version 2017-10-09
 */
@Controller
@RequestMapping(value = "${frontPath}/freeWaybill")
public class WebFreeWaybillController extends BaseController {

	@Autowired
	private WebFreeWaybillService freeWaybillService;
	@Autowired
	private WebStoreService webStoreService;
	
	@ModelAttribute
	public WebFreeWaybill get(@RequestParam(required=false) String id) {
		WebFreeWaybill entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = freeWaybillService.get(id);
		}
		if (entity == null){
			entity = new WebFreeWaybill();
		}
		return entity;
	}
	
	@RequestMapping(value = "isSatisfyFree")
	@ResponseBody
	public WebFreeWaybill isSatisfyFree(WebFreeWaybill freeWaybill,String provinceId, RedirectAttributes redirectAttributes) {
		freeWaybill.setReceiveAreaId(provinceId);
		return freeWaybillService.getWebFreeWaybillByArea(freeWaybill);
	}
	
	@RequestMapping(value = "isSatisfyFreeByStoreCode")
	@ResponseBody
	public WebFreeWaybill isSatisfyFreeByStoreCode(String storeCode,String provinceId, RedirectAttributes redirectAttributes) {
		WebStore WebStore = webStoreService.getByCode(storeCode);
		WebFreeWaybill freeWaybill = new WebFreeWaybill();
		freeWaybill.setReceiveAreaId(provinceId);
		freeWaybill.setSendAreaId(WebStore.getClassify());
		return freeWaybillService.getWebFreeWaybillByArea(freeWaybill);
	}
	
}